import React, { useState, } from 'react'
import { NotifyType } from '../../common/enums'
import basicStyle from './basic.scss'
import { clearData } from '../../source/saveData'
import { mergeStyle } from './mergeStyle'
import { notify } from '../../source/notify'

export const useClearData = () => {
    const [collection, setCollection] = useState('logs')
    const warpClearLogs = React.useCallback(() => {
        collection && clearData(collection)
            .then(({ message }) => {
                if (message) {
                    notify(NotifyType.error, '删除数据失败：' + message)
                } else {
                    notify(NotifyType.info, '删除数据成功')
                }
            })
    }, [collection])

    return (
        <div className={basicStyle.actions}>
            <input value={collection} onChange={ev => setCollection(ev.target.value)} />
            <button className={mergeStyle(basicStyle)} onClick={warpClearLogs}>
                Clear Data
        </button>
        </div>
    )
}